import 'package:flutter/material.dart';
import 'package:helloworld/pages/home_page.dart';
import 'package:helloworld/pages/my_page.dart';
import 'package:helloworld/pages/search_page.dart';
import 'package:helloworld/pages/travel_page.dart';

class TabNavigator extends StatefulWidget {
  TabNavigator({Key key}) : super(key: key);

  @override
  _TabNavigatorState createState() => _TabNavigatorState();
}

class _TabNavigatorState extends State<TabNavigator> {
  final PageController _controller = PageController(initialPage: 0);
  final _defaultColor = Colors.grey;
  final _activeColor = Colors.blue;
  int _currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        onPageChanged: (e) {
          setState(() {
            _currentIndex = e;
          });
        },
        controller: _controller,
        children: <Widget>[HomePage(), SearchPage(), TravelPage(), MyPage()],
      ),
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            _controller.jumpToPage(index);
            setState(() {
              _currentIndex = index;
            });
          },
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
                title: Text('首页',
                    style: TextStyle(
                        color:
                            _currentIndex != 0 ? _defaultColor : _activeColor)),
                icon: Icon(
                  Icons.home,
                  color: _defaultColor,
                ),
                activeIcon: Icon(
                  Icons.home,
                  color: _activeColor,
                )),
            BottomNavigationBarItem(
                title: Text('搜索',
                    style: TextStyle(
                        color:
                            _currentIndex != 1 ? _defaultColor : _activeColor)),
                icon: Icon(
                  Icons.search,
                  color: _defaultColor,
                ),
                activeIcon: Icon(
                  Icons.search,
                  color: _activeColor,
                )),
            BottomNavigationBarItem(
                title: Text('旅拍',
                    style: TextStyle(
                        color:
                            _currentIndex != 2 ? _defaultColor : _activeColor)),
                icon: Icon(
                  Icons.camera_alt,
                  color: _defaultColor,
                ),
                activeIcon: Icon(
                  Icons.camera_alt,
                  color: _activeColor,
                )),
            BottomNavigationBarItem(
                title: Text('我的',
                    style: TextStyle(
                        color:
                            _currentIndex != 3 ? _defaultColor : _activeColor)),
                icon: Icon(
                  Icons.account_circle,
                  color: _defaultColor,
                ),
                activeIcon: Icon(
                  Icons.account_circle,
                  color: _activeColor,
                )),
          ]),
    );
  }
}
